package com.linmo;

import com.linmo.historymanage.HistoryGuI;
import com.linmo.chatmanage.ChatJPanelGUI;
import com.linmo.personalcenter.PersonalPanelGUI;
import com.linmo.loginmanage.User;
import com.linmo.questionmatcher.QuestionMatcher;

import javax.swing.*;
import java.awt.*;

public class MainJFrameGUI extends JFrame {
    private JTabbedPane tabbedPane;
    private ChatJPanelGUI chatJPanelGUI;
    private HistoryGuI historyGuI;
    private PersonalPanelGUI personalPanelGUI;
    private QuestionMatcher questionMatcher;

    public MainJFrameGUI(User user) {
        questionMatcher = new QuestionMatcher();

        setTitle("Linmo人工智能聊天机器人");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 设置窗口居中显示

        //初始化各个模块
        chatJPanelGUI = new ChatJPanelGUI(user.getId());
        historyGuI = new HistoryGuI(user.getId(),chatJPanelGUI);
        personalPanelGUI = new PersonalPanelGUI(user,MainJFrameGUI.this);

        // 创建标签页
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("会话区", chatJPanelGUI);
        tabbedPane.addTab("历史会话", historyGuI); // 集成历史会话模块
        tabbedPane.addTab("个人中心", personalPanelGUI);

        add(tabbedPane, BorderLayout.CENTER);

        // 添加标签页变化监听器
        tabbedPane.addChangeListener(e -> {
            int selectedIndex = tabbedPane.getSelectedIndex();
            if (selectedIndex == 1) { // 历史会话标签页的索引为1
                // 结束当前会话并保存
                //chatJPanelGUI.endCurrentSessionAndSave();

                // 重新加载历史会话记录
                historyGuI.reloadHistoryRecords(user.getId());
            }
        });
    }

    // 提供方法获取 QuestionMatcher 实例
    public QuestionMatcher getQuestionMatcher() {
        return questionMatcher;
    }

    @Override
    public void dispose() {
        // 关闭线程池
        chatJPanelGUI.shutdown();
        historyGuI.shutdown();
        super.dispose();
    }

//    private JPanel createPersonalPanel() {
//        // 个人中心实现
//        JPanel panel = new JPanel();
//        panel.add(new JLabel("欢迎, 用户名"));
//        return panel;
//    }

}